package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.dish.DishDTO;
import com.sky.dto.dish.DishPageDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.dish.DishService;
import com.sky.utils.UploadFile;
import com.sky.vo.DishPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @Author 褚金龙
 * @Date 2024/9/1 20:37
 */
@RequestMapping("/admin/dish")
@RestController
@CrossOrigin("*")
@Slf4j
@Api(tags = "菜品相关接口")

public class DishController {
   @Autowired
    private DishService dishPageService;
   @Autowired
   private UploadFile uploadFile;
   @Autowired
   private RedisTemplate redisTemplate;
   @ApiOperation("分页查询")
   @GetMapping("/page")
   public Result<PageResult> dishPage(DishPageDTO dishPageDTO){
         log.info("{}",dishPageDTO);
      PageResult pageResult = dishPageService.dishPageService(dishPageDTO);
       return Result.success(pageResult);
   }



   @ApiOperation("套餐、菜品分类数据查询")
    @GetMapping("/{type}")
    public Result<PageResult>  dishType(@PathVariable Integer type){
      // log.info("type{}",type);
       //返回需要它的分类id和分类名称给前端
     PageResult pageResult=  dishPageService.dishTypeService(type);
     return Result.success(pageResult);
   }


    /**
     * 图片上传
     */

    @PostMapping("/upload")
    @ApiOperation("图片上传")

    public Result<String>upload(MultipartFile image) throws IOException {
      if(image.isEmpty()) {
          return Result.error(MessageConstant.UPLOAD_FAILED);
      }
        String url = uploadFile.Upload(image);

        return Result.success(url);
      }


    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping()
    public Result addDish(@RequestBody DishDTO dishDTO){

        dishPageService.addDishService(dishDTO);
        //清理缓存数据
        String key="dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping()
    @CacheEvict(cacheNames ="dishCache" ,allEntries=true)  //删除所有缓存数据
   public Result deleteDish(@RequestParam List<Long> ids){
        //log.info("ids{}", ids);
        dishPageService.deleteAll(ids);
        //将所有的菜品缓存数据清理
        //获取所有dish_*开头的菜品
       // cleanCache("dish_*");
        return Result.success();
   }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
   @ApiOperation("修改菜品状态")
    @CacheEvict(cacheNames = "dishCache",key="#id")
   public Result statusUpdate(@PathVariable Integer status, @RequestBody Integer id){
     //   log.info("status{},id{}",status,id);
       dishPageService.statusUpdateService(status,id);

        //将所有的菜品缓存数据清理
        //获取所有dish_*开头的菜品
       // cleanCache("dish_*");

        return Result.success();
   }

    /**
     * 回显菜品数据
     * @param id
     * @return
     */
   @GetMapping("/query/{id}")
   @ApiOperation("回显菜品数据")
   public Result<DishDTO> getDish(@PathVariable Long id){

       DishDTO dishDTO = dishPageService.getById(id);
       return  Result.success(dishDTO);
   }

   @PutMapping("/update")
   @ApiOperation("修改菜品和口味数据")
   @CacheEvict(cacheNames ="dishCache" ,allEntries=true)  //删除所有缓存数据
   public Result updateDish(@RequestBody DishDTO dishDTO){
      // log.info("dishDTO{}",dishDTO);
        dishPageService.updateDishService(dishDTO);
       //将所有的菜品缓存数据清理
       //获取所有dish_*开头的菜品
      // cleanCache("dish_*");

       return Result.success();
   }


    /**
     *清除缓存数据
     * @param pattern
     */

  /* private void  cleanCache(String pattern){
       Set keys = redisTemplate.keys(pattern);
       redisTemplate.delete(keys);

   }*/

}
